<?php
//require_once "sprder.php";
/**
 * @author baby&star
 *
 *
 */

class belle extends sprder{
	//TODO - Insert your code here
	//private $host    = "";
	
	private $webRoot = "";
	
	function __construct($arr) {
		//print_r($arr);exit;
		//TODO - Insert your code here
		parent::__construct($arr);
		
		//$this->_url = "http://www.belle.com.cn/ss2012/index_.php";
		//echo $this->_url;exit;
		
		$domain = parse_url($this->_url);
		
		$this->host = $domain["scheme"]."//".$domain["host"];
		
		$this->webRoot = explode('/',$this->_url);
		
		array_pop($this->webRoot);
		
		$this->webRoot = implode('/',$this->webRoot);
		//echo $this->host;exit;
	}
	function start(){
		
		$cts = $this->getWebContent($this->_url);
		
		//取所有的图片
		$imgContents = preg_match_all("/<img.+?src=[\"\']?([\d\D]+?)[\"\']?\s+.+?\/{0,1}>/i",$cts,$imgRs);
		
		//处理数据
		$data = "";
		
		foreach($imgRs[1] as $k=>$v){
			
			$v = explode(" ",$v);
			
			if(is_array($v)){
				$v = $v[0];
			}
			
			$v = str_replace('"', '', $v);
			
			$v = str_replace("'", '', $v);
			
			$str = substr($v,0,1);
			
			if($str == "/"){
				$v = $this->host.'/'.$v;
			}else{
				
				$dirLevel = explode('..',$v);
				
				$len = count($dirLevel);
				
				$webUrl = $this->webRoot;
				if($len>2){
					$len--;
					
					$webUrl = explode('/',$this->webRoot);
					
					for($i=0;$i<$len;$i++){
						
						array_pop($webUrl);
						
						array_shift($dirLevel);
					}
					
					$webUrl = implode('/',$webUrl);
					
					$v = implode('',$dirLevel);
					
					$v = ltrim($v,'/');
				}
				
				
				
				$v = $webUrl.'/'.$v;
				
				
			}
			
			$data[] = $v;
			
			//下载文件
			$filePath = $this->downFile($v);
			
			//保存
			$this->save($filePath);
		}
		$this->readCss($cts);
		exit;
	}
	function readCss($str){
		$strstr = "/<link.+href=\"(.+\.css)\"\s+\/>/";
		preg_match_all($strstr,$str,$cssRs);
		$cssRs = $cssRs[1];
		array_shift($cssRs);
		foreach($cssRs as $k=>$v){
			$str = substr($v,0,1);
				
			if($str == "/"){
				$v = $this->host.'/'.$v;
				$this->downImgCss($v,$this->host);
			}else{
				$v = $this->webRoot.'/'.$v;
				$this->downImgCss($v,$this->webRoot);
			}
			
			$data[] = $v;
		}
		
	}
	private function downImgCss($v,$host){
		
		$strstr = "/background-image:url\([\'|\"](.+\.png)[\'|\"]\);/";
		
		$content = $this->getWebContent($v);
				
		preg_match_all($strstr,$content,$cssImg);
		
		if(is_array($cssImg)){
			
			foreach($cssImg[1] as $k=>$v){
				
				$v = str_replace('../','',$v);
				
				$v = $host.'/'.$v;
				
				$this->downFile($v);
			}
		}
		return false;
	}
	//保存
	private function save($imgPath){
		if(empty($imgPath)){
			return false;
		}
		$db = new brand_products();
		$arr = array(
				'brandId'=>$this->_id,
				//'price'=>$price,
				//'detailHref'=>$detailHref,
				//'name'=>$name,
				'imgPath'=>$imgPath,
				'createDate'=>date('Y-m-d H:i:s'),
				'deleted'=>'0'
		);
		
		return $db->insertData($arr);
	}

}
